package fr.jade.fraclite.control.content;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.InitializationContext;
import fr.jade.fraclite.api.Controller;
import fr.jade.fraclite.api.control.SuperControllerNotifier;

/**
 * Provides a basic implementation of the {@link SuperControllerNotifier}
 * interface.
 */

public class BasicSuperController implements Controller, SuperControllerNotifier {


  /**
   * The parents of the component to which this controller object belongs.
   */
  public Component[] fcParents;
  
  public void initFcController(InitializationContext ic) throws InstantiationException {

  }

  public Component[] getFcSuperComponents () {
    if (fcParents == null) {
      return new Component[0];
    } else {
      return fcParents;
    }
  }

  public void addedToFc (final Component parent) {
    int length = fcParents == null ? 1 : fcParents.length + 1;
    Component[] parents = new Component[length];
    if (fcParents != null) {
      System.arraycopy(fcParents, 0, parents, 1, fcParents.length);
    }
    parents[0] = parent;
    fcParents = parents;
  }

  public void removedFromFc (final Component parent) {
    int length = fcParents.length - 1;
    if (length == 0) {
      fcParents = null;
    } else {
      Component[] parents = new Component[length];
      int i = 0;
      for (int j = 0; j < fcParents.length; ++j) {
        if (!fcParents[j].equals(parent)) {
          parents[i++] = fcParents[j];
        }
      }
      fcParents = parents;
    }
  }

}
